package org.lex.dbq.view.ddl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public abstract class TableDDLPane<T> extends DDLPane<T> {
	protected TableViewer table = null;
	private List<TableColumn> columns = new ArrayList<TableColumn>();
	private final String[] columnNames;

	public TableDDLPane(Composite parent, int style, String[] columnNames) {
		super(parent, style);
		this.columnNames = columnNames;
		this.initialize();
	}

	public void setInput(Object[] input) {
		this.table.setInput(input);
		for (TableColumn c : columns) {
			c.pack();
		}
	}

	public void setInput(Collection<T> input) {
		this.setInput((T[]) input.toArray());
	}

	private void initialize() {
		this.setLayout(new FillLayout());

		Table tt = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
		tt.setHeaderVisible(true);
		tt.setLinesVisible(true);
		for (String c : columnNames) {
			TableColumn tc = new TableColumn(tt, SWT.NONE);
			tc.setText(c);
			tc.pack();
			columns.add(tc);
		}

		table = new TableViewer(tt);
		table.setContentProvider(new ArrayContentProvider());
	}
}
